package com.msmall.community.common;

import java.util.Iterator;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.msmall.base.cache.CacheFactory;
import com.msmall.base.util.Constants;
import com.msmall.base.util.Initialize;
import com.msmall.community.ApplicationManager;
import com.msmall.community.bbs.service.BoardManager;
import com.msmall.community.common.model.Config;
import com.msmall.community.common.model.Link;
import com.msmall.community.common.model.RankRule;
import com.msmall.community.common.model.ScoreRule;
import com.msmall.community.common.model.UserAgent;
import com.msmall.community.common.service.ConfigManager;
import com.msmall.community.common.service.LinkManager;
import com.msmall.community.common.service.RankRuleManager;
import com.msmall.community.common.service.ScoreRuleManager;
import com.msmall.community.common.service.UserAgentManager;
import com.msmall.community.dpt.service.DepartmentManager;
import com.msmall.community.dpt.service.SegmentManager;
import com.msmall.community.keyword.service.KeywordManager;
import com.msmall.community.play.service.PlayManager;

/**
 * 
 * @author zhzy
 *
 */
public class CommunityInitialize extends Initialize{

	private static final Log logger = LogFactory.getLog(CommunityInitialize.class);
	
	public static void refresh()throws Exception {
		refreshUserAgent();
		refreshScoreRule();
		refreshRankRule();
		refreshConfig();
		refreshPlay();
		refreshLink();
		refreshDepartment();
		refreshSegment();
		refreshBoard();
		refreshKeyword();
	}

	/**
	 * 初始化系统基本信息,包括ENABLE可能为UNAVALIABLE
	 * @throws Exception 
	 */
	
	public static void refreshConfig() throws Exception{
		ConfigManager configManager = (ConfigManager)ApplicationManager.appContext.getBean("configManager");
		List list = configManager.getAll(Config.class);
		Configuration.add(list);
	}
	/**
	 * 加载系统积分规则,包括ENABLE可能为UNAVALIABLE
	 */
	public static void refreshScoreRule(){
		ScoreRuleManager scoreRuleManager = (ScoreRuleManager)ApplicationManager.appContext.getBean("scoreRuleManager");
		List list = scoreRuleManager.getAll(ScoreRule.class);
		if(null != list){
			Iterator it = list.iterator();
			while(it.hasNext()){
				ScoreRule sr = (ScoreRule)it.next();
				CacheFactory.getInstance().getCache(Constants.CACHE_NAME_SCORERULE).put(sr.getScoreKey(), sr);
			}
		}else{
			logger.error("No ScoreRule Exist");
		}
	}
	/**
	 * 加载系统等级规则,包括ENABLE可能为UNAVALIABLE
	 */
	public static void refreshRankRule(){
		RankRuleManager rankRuleManager = (RankRuleManager)ApplicationManager.appContext.getBean("rankRuleManager");
		List list = rankRuleManager.getAll(RankRule.class);
		if(null != list){
			Iterator it = list.iterator();
			while(it.hasNext()){
				RankRule rr = (RankRule)it.next();
				CacheFactory.getInstance().getCache(Constants.CACHE_NAME_SCORERULE).put(rr.getLevels(), rr);
			}
		}else{
			logger.error("No ScoreRule Exist");
		}
	}
	
	/**
	 * 初始化所有 UserAgent,包括ENABLE可能为UNAVALIABLE
	 */
	public static void refreshUserAgent(){
		try{
			UserAgentManager userAgentManager = (UserAgentManager)ApplicationManager.appContext.getBean("userAgentManager");
			List list = userAgentManager.getAll();
			if(list == null || list.size() <= 0){
				logger.debug("NO UserAgent !");
				return;
			}
			Iterator it = list.iterator();
			while (it.hasNext()) {
				UserAgent bean = (UserAgent) it.next();
				bean.setAgentValue(bean.getAgentValue().toLowerCase()); //为了以后比较时都用小写比较
				bean.setAgentType(bean.getAgentType().toLowerCase());
			}
			CacheFactory.getInstance().getCache(Constants.CACHE_NAME_COMMON).put(Constants.CACHE_KEY_USER_AGENT_LIST, list);
		}catch(Exception e){
			logger.error(e);
		}
	}
	
	/**
	 * 初始化所以组件PLAY
	 */
	public static void refreshPlay(){
		try{
			PlayManager playManager = (PlayManager) ApplicationManager.appContext.getBean("playManager");
			List list = playManager.getAllPlays();
			if(list == null || list.size() <= 0){
				logger.debug("NO Play !");
				return;
			}
			CacheFactory.getInstance().getCache(Constants.CACHE_NAME_COMMON).put(Constants.CACHE_KEY_PLAY_LIST, list);
		}catch(Exception e){
			logger.error(e);
		}
	}
	/**
	 * 刷新友情链接
	 */
	public static void refreshLink(){
		try{
			LinkManager manager = (LinkManager) ApplicationManager.appContext.getBean("linkManager");
			List<Link> list = manager.loadAllEnabled();
			if(list == null || list.size() <= 0){
				logger.debug("NO Link !");
				return;
			}
			CacheFactory.getInstance().getCache(Constants.CACHE_NAME_COMMON).put(Constants.CACHE_KEY_LINK_LIST, list);
		}catch(Exception e){
			logger.error(e);
		}
	}
	
	/**
	 * 刷新栏目
	 */
	public static void refreshDepartment(){
		try{
			DepartmentManager manager = (DepartmentManager) ApplicationManager.appContext.getBean("departmentManager");
			manager.refresh();
		}catch(Exception e){
			logger.error(e);
		}
	}
	/**
	 * 刷新次要栏目
	 */
	public static void refreshSegment(){
		try{
			SegmentManager manager = (SegmentManager) ApplicationManager.appContext.getBean("segmentManager");
			manager.refresh();
		}catch(Exception e){
			logger.error(e);
		}
	}
	/**
	 * 加载所有版区信息,包括版主
	 */
	public static void refreshBoard(){
		try{
			BoardManager boardManager = (BoardManager)ApplicationManager.appContext.getBean("boardManager");
			boardManager.refresh();
			logger.info("refresh Boards");
		}catch(Exception e){
			logger.error(e);
		}
	}
	/**
	 * 载入关键字
	 */
	public static void refreshKeyword(){
		try{
			KeywordManager manager = (KeywordManager)ApplicationManager.appContext.getBean("keywordManager");
			manager.refresh();
			logger.info("refresh keyword");
		}catch(Exception e){
			logger.error(e);
		}
	}
}